#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include"playscene.h"
#include<QSound>

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    //配置选择关卡的场景
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("dajiba");

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);
    QMenu *starMenu=bar->addMenu("now");
    QAction *quitAction=starMenu->addAction("tuichu");

    //点击关闭
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //选择按钮的音效
    QSound *chooseSound=new QSound(":/res/TapButtonSound.wav",this);
    //返回按钮的音效
    QSound *backSound=new QSound(":/res/TapButtonSound.wav",this);

    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    //点击返回
    connect(backBtn,&MyPushButton::clicked,[=](){

        //返回音效
        backSound->play();

        //告诉主场景我返回了，主场景监听ChooseLevelScene的返回按钮
        QTimer::singleShot(500,this,[=](){
             emit this->chooseSceneBack();
        });
        //emit this->chooseSceneBack();
    });

    //创建选择关卡的按钮
    //一个循环写一个矩阵
    for(int i=0;i<20;i++){
        MyPushButton *menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+i%4*70,130+i/4*70);

        //监听每个按钮的点击事件
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //选择按钮音效
            chooseSound->play();

            QString str=QString("select %1").arg(i+1);
            qDebug()<<str;



            //进入到游戏的场景
            this->hide();
            pScene=new PlayScene(i+1);
            //设置游戏场景的初始位置
            pScene->setGeometry(this->geometry());


            pScene->show();



            //PlayScene 的返回按钮监听，删除该scene并且将指针指向空
             connect(pScene,&PlayScene::chooseSceneBack,[=](){
                this->setGeometry(pScene->geometry());
                this->show();
                delete pScene;
                pScene=NULL;
             });
        });

        //鼠标穿透事件

        QLabel *label=new QLabel;
        label->setParent(this);

        //设置标签的大小和位置
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(1+i));
        label->move(25+i%4*70,130+i/4*70);

        //设置label上的文字对齐方式
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);

        //设置让鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);

        connect(menuBtn,&MyPushButton::clicked,[=](){
            if(pScene==NULL){
                this->hide();
                //先这么写，而后去自定义有参构造
                pScene=new PlayScene(i+1);
                pScene->show();


            }
        });
    }
}
void ChooseLevelScene::paintEvent(QPaintEvent *){

     QPainter painter(this);
     QPixmap pix;
     pix.load(":/res/OtherSceneBg.png");
     painter.drawPixmap(0,0,this->width(),this->height(),pix);

     pix.load(":/res/Title.png");
     painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}
